package com.brayan.webapp.controller.general.i18n;

import java.io.Serializable;

import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;
import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.http.HttpServletRequest;

import com.brayan.webapp.model.general.country.Country;
import com.brayan.webapp.model.general.i18n.Locale;
import com.brayan.webapp.repository.general.i18n.LocaleRepository;
import com.brayan.webapp.service.general.i18n.I18NBean;
import com.brayan.webapp.service.general.i18n.LocaleBean;

@Named
@SessionScoped
public class I18N implements Serializable {

	private static final long serialVersionUID = 979842074853622353L;

	@Inject
	I18NBean i18nService;
	@Inject
	LocaleBean localeService;
	@Inject
	LocaleRepository localeRepo;

	private Locale locale;

	private String DEFAULT_LOCALE = "en";

	public Locale getLocale() {
		return locale;
	}

	public void setLocale(Locale locale) {
		this.locale = locale;
	}

	/**
	 * Cuts out the first language and country code in the list of accepted
	 * languages.
	 * 
	 * @param localeCode
	 *            The list of locale code separated by semi-colon.
	 */
	private void setLocaleByAcceptedLanguage(String localeCode) {
		int occurenceOfComma = localeCode.indexOf(",");
		String localeString = localeCode.substring(0, occurenceOfComma == -1 ? localeCode.length() : occurenceOfComma);
		
		Locale operatingLocale = localeService.getLocaleByLocaleCode(localeString);
		if(operatingLocale != null)		
			setLocale(operatingLocale);
		else{
			// try with parent locale: e.g. de-CH --> de
			operatingLocale = localeService.getLocaleByLocaleCode(localeCode.substring(0, 2));
			if(operatingLocale != null)
				setLocale(operatingLocale);
			else
				setDefaultLocale();
		}

		java.util.Locale javaLocale = new java.util.Locale(localeString);
		FacesContext.getCurrentInstance().getViewRoot().setLocale(javaLocale);
	}
	
	/**
	 * Set to default English Locale.
	 */
	private void setDefaultLocale(){ 
		Locale locale = localeRepo.getLocaleByLanguage(DEFAULT_LOCALE );
		setLocale(locale);
	}

	@PostConstruct
	private void init() {
		HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext()
				.getRequest();
		String acceptLanguage = request.getHeader("accept-language");
		if(acceptLanguage != null && !acceptLanguage.trim().isEmpty())
			setLocaleByAcceptedLanguage(acceptLanguage);
		else
			setLocaleByAcceptedLanguage(DEFAULT_LOCALE);
	}

	/**
	 * Return the translation of the currently used language. This is set either
	 * by the browsers language or the session-saved language.
	 * 
	 * @param token
	 *            The token to search the database for.
	 * @return
	 */
	public String getTrans(String token) {
		String trans = i18nService.getTrans(token, getLocale());
		return trans;
	}
	
	
	/**
	 * Use this method to set the session language.
	 * @param e
	 */
	public void localeChanged(ValueChangeEvent e) {
		String newLocaleId = e.getNewValue().toString();
		Long localeId = Long.parseLong(newLocaleId);
		Locale locale = localeRepo.getLocaleById(localeId);
		setLocale(locale);
		
		java.util.Locale javaLocale = new java.util.Locale(newLocaleId);
		FacesContext.getCurrentInstance().getViewRoot().setLocale(javaLocale);
	}

}
